<?php

class Jd_task_jd_model extends MY_Model {





    public function __construct()
    {
        $this->modelename='Jd_task_jd_model';
        $this->tablename='task_jd';
        parent::__construct();
    }


    /**
     *
     * 商户添加任务
     * @param array $array
     * @return INT
     */
    public function  addtask($array=array(),$type=1){
        $array['business_id']=$this->userId;
        $array['addtime']=time();
        $array['status']=$type;
        return  $r= $this->add($this->tablename,$array);
    }

    /**
     *
     * 商户添加定时任务
     * @param array $array
     * @return INT
     */
    public function  addtasktime($array=array()){
        $array['business_id']=$this->userId;
        $array['addtime']=time();
        $array['status']=1;
        return  $r= $this->add('task_jd_time',$array);
    }




    /**
     * 获取发布成功且设置了发布间隔 但是未完成的任务
     */
    public function getwaittask(){
        $sql="select * from jd_task_jd  where  `status` =2   ";
        $result=  $this->GetBySql($sql);
        return $result;
    }


    /**
     * 更新最后的推送时间
     */
    public function UpdatelastSendTime($isstr=array()){
        $time=time();
        if(is_array($isstr)){
            $sql="update  jd_task_jd  set lastsendtime= $time where id in  (".implode($isstr,",").")";
        }else{
            $sql="update  jd_task_jd  set lastsendtime= $time where id =$isstr";
        }
       return $r= $this->db->query($sql);
    }


    /**
     * 更新推送间隔并进入推送池
     */
    public function UpdatelastSendInterval($id,$interval,$team=0){
        $sql="update  jd_task_jd  set `interval`= $interval,`team`='$team' where id =$id";

        return $r= $this->db->query($sql);
    }

    public function UpdateStatus($id,$Status){
        $sql="update  jd_task_jd  set `status`= $Status where id =$id";
        return $r= $this->db->query($sql);
    }


    /**
     * 任务更新完成单数和状态
     */
    public function updatetask($id,$finish_nub=1){
         $sql="update jd_task_jd set finish_nub=finish_nub+$finish_nub where id=$id ";
         $r=$this->db->query($sql);
         if(!$r){
             return false;
         }
         $row=$this->getonebyid($id);
         if($row['finish_nub']==$row['number']){
             $finishtime=time();
             $sql="select sum(real_price) as 'real_price' from  jd_task_jd_list where taskid=".$id;
             $real_price= $this->GetBySql($sql);
             $real_price=$real_price[0]['real_price'];
             if(empty($real_price)){
                 return false;
             }
             $imbalance=$row['pay_price']*$row['number']*$row['each_single']-$real_price;
             if(empty($imbalance)){
                 $imbalance=0;
             }
             $sql="update jd_task_jd set `status`=3 ,finishtime=$finishtime , real_money=$real_price,imbalance=$imbalance where id=$id ";
             return  $r=$this->db->query($sql);
         }
         return true;
    }


    /**
     * 更新最终使用的实际金额
     */
    public function updatetaskmoney($taskid){
        $sql="select sum(real_price) as 'real_price' from  jd_task_jd_list where taskid=".$taskid;
        $real_price= $this->GetBySql($sql,0);
        $real_price=$real_price['real_price'];
        $row=$this->getonebyid($taskid);
        $imbalance=$row['pay_price']*$row['number']*$row['each_single']-$real_price;
        if(empty($imbalance)){
            $imbalance=0;
        }
        $sql="update jd_task_jd set real_money=$real_price,imbalance=$imbalance where id=$taskid ";
        return  $r=$this->db->query($sql);
    }


    /**
     * 获取最先发布的任务
     */
    public function getnewtask($epage=10){
        $sql="select id from jd_task_jd  where  `status` =1    AND `shop_name`<>'' group by `shop_name`  order by  id  asc  limit 0,".$epage;
        $result=  $this->GetBySql($sql);
        return $result;
    }


    public function getonebyid($id, $select = '*')
    {
        return parent::getonebyid($id, $select); // TODO: Change the autogenerated stub
    }

    public function taskupdate($row)
    {
        $table=$this->tablename;
        $primary="id";
        $id=$row['id'];

        unset($row['id']);
        return $this->update($table, $primary, $id, $row); // TODO: Change the autogenerated stub
    }

    /**
     * 批量完成高留评订单
     */
    public function finnshtypeh($taskid,$row){

        $user_id=$this->user_id;
        $huntername=$this->username;
        $team=$this->userinfo['team'];

        $allreal_price=0;
        foreach( $row as $id=>$v){
            $listid=$v['A'];
            $real_price=$v['I'];
            $awz=$v['F'];
            $orderid=$v['D'];
            $finishtime=time();
            @$sql="update  jd_task_jd_list set `status`=2,hunter_id=$user_id, `real_price`=$real_price ,`awz`='$awz',  `orderid`='$orderid' ,`finishtime`=$finishtime,
`team`='$team',hunter_name='$huntername'  where id =$listid";

            $r= $this->db->query($sql);
            if(!$r){
                return false;
            }
            $allreal_price=$allreal_price+$real_price;
        }
        $finishtime=time();
        $imbalance=0;
        $finish_nub=$id+1;
        $sql="update jd_task_jd set `status`=3 ,finish_nub= $finish_nub, finishtime=$finishtime , real_money=$allreal_price,imbalance=$imbalance,lastsendtime= $finishtime where id=$taskid ";
        return  $r=$this->db->query($sql);
    }





    /**
     * 获取一个月内所有的放单量
     */
    public function monthsend(){
        $date=date('Y-m-d');
        $firstday = date('Y-m-01 00:00:00', strtotime($date));
        $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));
        $firstday=strtotime($firstday);
        $lastday=strtotime($lastday);
        $sql="select sum(`number`) as 'number',business_id from  jd_task_jd_list where addtime>=$firstday AND addtime<$lastday GROUP BY business_id";
       return  $this->GetBySql($sql);
    }


    /**
     * 获取定时任务
     */
    public function gettimetask(){
        $now=time();
        $sql="select * from  jd_task_jd where releasetime<$now AND releasetime>1585039227   AND issend=0  AND `status`<3";
        return  $this->GetBySql($sql);
    }



    /**
     * 获指定用户的取定时任务
     */
    public function getmytimetask($user_id){
        $now=time();
        $sql="select sum(`number`*each_single*pay_price) as 'money',sum(gold) as 'gold' from  jd_task_jd where business_id=$user_id AND releasetime>1585039227   AND issend=0  AND `status`<3";
        return  $this->GetBySql($sql,0);
    }



    /**
     * 更新定时任务为已发布
     */
    public function updaetime($id){
        $sql="update jd_task_jd set `issend`=1,`status`=1 where  id= $id";
        return  $r=$this->db->query($sql);
    }





}
